<?php


namespace app\model;
use think\Model;
use think\model\concern\SoftDelete;

class Cart extends Model
{

    public function getQuantity($user_id){
        $res = Cart::where('id',$user_id)->field('quantity')->find();
        return $res;
    }

    public function checkCart($goods_id){
        $res = Cart::where('goods_id',$goods_id)->find();
        return $res;
    }

    public function addCart($data){
        $res = Cart::create($data);
        return $res;
    }

    public function updateQuantity($id){
        Cart::where('goods_id',$id)->inc('quantity')->update();
    }

    public function delOne($id){
        $res = Cart::where('goods_id',$id)->delete();
        return $res;
    }

    //购买所有
    public function buyAll(){
        //获取所有购物车的商品id和数量
        $data = Cart::where('id','<>',-1)->field('goods_id')->field('quantity')->select();
        Cart::where('id','<>',-1)->delete();
        return $data;
    }

}